package com.dst.controller;

import com.dst.eneity.Category;
import com.dst.eneity.Tea;
import com.dst.service.impl.CategoryServiceImpl;
import com.dst.service.impl.TeaServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/tea")
public class TeasController {
    private TeaServiceImpl ts = new TeaServiceImpl();
//    @RequestMapping("/selectAll")
    public String selectAll(Model model){
        List<Tea> teas = ts.selectAll();
        model.addAttribute("teas",teas);
        return "/tealist.jsp";
    }
    @RequestMapping("/delete")
    public String delete(int id){
        ts.delete(id);
        return "redirect:/tea/selectAll.do";
    }
    @RequestMapping("/add")
    public String add(Tea tea) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(System.currentTimeMillis());
        Date date = dateFormat.parse(format);
        tea.setAddtime(date);
        System.out.println(tea);
        ts.add(tea);
        return "redirect:/tea/selectAll.do";
    }
    @RequestMapping("/update")
    public String update(Tea tea) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = dateFormat.format(System.currentTimeMillis());
        Date date = dateFormat.parse(format);
        tea.setAddtime(date);
        ts.update(tea);
        return "redirect:/tea/selectAll.do";
    }
    @RequestMapping("/selectById")
    public String selectById(int id,Model model){
        Tea tea = ts.selectById(id);
        CategoryServiceImpl categoryService = new CategoryServiceImpl();
        List<Category> categories = categoryService.selectAll();
        model.addAttribute("categories",categories);
        model.addAttribute("tea",tea);
        return "/updateTea.jsp";
    }
    @RequestMapping("/deleteBatch")
    public String deleteBathc(int [] ids){
        ts.deleteBatch(ids);
        return "redirect:/tea/selectAll.do";
    }
    @RequestMapping("/selectTeaAll")
    public String selectTeaAll(String name,Model model,String curPage){
        int curpage=1;
        if(curPage!=null){
            curpage=Integer.parseInt(curPage);
        }
        int pageSize=4;
        PageInfo<Tea> teas = ts.selectAllTea(name);
        model.addAttribute("teas",teas);
        return "/tealist.jsp";
    }
    @RequestMapping("/selectAll")
    public String selectLimitAll(Model model,String curPage){
        int curpage=1;
        if(curPage!=null){
            curpage=Integer.parseInt(curPage);
        }
        int pageSize=4;
        PageInfo<Tea> pageInfo = ts.selectLimitAll(curpage, pageSize);
        model.addAttribute("teas",pageInfo);
        return "/tealist.jsp";
    }
    @RequestMapping("/selectLikeAll")
    public String selectLikeAll(String name , Double price, Model model,Integer curPage){
        int curpage=1;
        if(curPage!=null){
            curpage=curPage;
        }
        int pageSize=4;
        PageInfo<Tea> pageInfo = ts.selectLikeAll(name, price);
        model.addAttribute("teas",pageInfo);
        return "/tealist.jsp";
    }
}
